package com.liangtian.mdm.system.ugroup.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.exception.TransactionalException;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.dm.common.page.JsonWrap;
import com.liangtian.mdm.util.RequestUtils;
import com.liangtian.mdm.system.log.SysLogUtil;
import com.liangtian.mdm.system.log.model.SysLogEnum;
import com.liangtian.mdm.system.log.service.SysLogService;
import com.liangtian.mdm.system.ugroup.model.SysUgroup;
import com.liangtian.mdm.system.ugroup.service.SysUgroupService;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.util.ConstantBean;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Encoding    :  UTF-8</p>
 * <p>Package Name: com.liangtian.mdm.system.user.controller </p>
 * <p>Project Name: dm </p>
 * <p>Copyright   :  Copyright(c) 2016 石化盈科信息技术有限责任公司 </p>
 * <p>Created Time: 2016-08-31</p>
 * <p>Description : 用户实体</p>
 *
 * @author : byr
 * @version :  1.0
 */
@Controller
@RequestMapping("/sysUgroup")
public class SysUgroupController extends BaseController {

    //注入用户服务接口
    @Autowired
    SysUgroupService sysUgroupServiceImpl;

    //日志服务接口对象
    @Autowired
    SysLogService sysLogServiceImpl;

    //当前用户
    ////@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    /*
     * @param Map 对象
     * @return
     * @desc 获取用户组的全部数据
     */
    @RequestMapping(value = "/queryAllUgroupList", method = RequestMethod.POST)
    @ResponseBody
    public String queryAllUgroupList(HttpServletRequest request) {  //JSONObject
        //查询条件
        Map<String, Object> param = RequestUtils.paramToMap(request);
        System.out.println("=========queryList==========");
        System.out.println(JSON.toJSONString(param));
        PageInfo pageInfo = sysUgroupServiceImpl.queryAllUgroupList(param);

        List l = pageInfo.getList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", l);//JSONArray
        jsonObject.put("total", pageInfo.getTotal());//总记录数
        return JsonWrap.toJSONString(jsonObject);
    }

    /*
     * @param Map 对象
     * @return
     * @desc 获取某个用户组用户
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/queryUsersList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JSONObject queryUsersList(HttpServletRequest request, SysUgroup sysUgroup) {

        Map paramMap = RequestUtils.paramToMap(request, SysUgroup.class, false);

        PageInfo pageInfo = sysUgroupServiceImpl.selectUserList(paramMap);
        List l = pageInfo.getList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", l);// JSONArray
        jsonObject.put("total", pageInfo.getTotal());// 鎬昏褰曟暟
        return jsonObject;
    }

    /*
     * @param String 对象
     * @return
     * @desc 根据用户组ID查询用户组
     */
    @RequestMapping(value = "/queryone", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryone(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String groupid = request.getParameter("groupid");
        SysUgroup sysUgroup2 = new SysUgroup();
        try {
            sysUgroup2 = sysUgroupServiceImpl.queryOne(groupid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("group", sysUgroup2);
        return jsonObject;
    }

    /**
     * 新增用户组
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/insertUgroup", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject insertUgroup(HttpServletRequest request) throws TransactionalException {
        Map<String, Object> param = RequestUtils.paramToMap(request);
        boolean ret = false;
    	
       /* //获取session中用户
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        SysLog sysLog = new SysLog();
        Date date = new Date();
        sysLog.setUserName(user.getUserName());
        sysLog.setBrowserType(BrowserUtils.getBrowserName(request));
        sysLog.setOperationDate(date);*/
        try {
            ret = sysUgroupServiceImpl.insertUgroup(param);
            SysLogUtil.insertLog("新增用户组",
                    SysLogEnum.SYS_UGROUP.getCode(), SysLogEnum.XZ.getCode(), request);
           /*// if (ret) {
                sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",新增用户组成功");
           // }
            //成功记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
        } catch (Exception e) {
           /* sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",新增用户组失败");
            //失败记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
            e.printStackTrace();
        }
        int f = ret == false ? 1 : 0;
        ResultObject result = new ResultObject(f, "新增成功", "新增失败");
        return result;
    }

    /**
     * 批量新增用户组用户
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/insertUgroupUser", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject insertUgroupUser(HttpServletRequest request) throws TransactionalException {
        Map<String, Object> param = RequestUtils.paramToMap(request);
        String id = request.getParameter("ids");
        String[] ids = id.split(",");
        param.put("ids", ids);
        boolean ret = false;
    	
        /*//获取session中用户
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        SysLog sysLog = new SysLog();
        Date date = new Date();
        sysLog.setUserName(user.getUserName());
        sysLog.setBrowserType(BrowserUtils.getBrowserName(request));
        sysLog.setOperationDate(date);*/
        try {
            ret = sysUgroupServiceImpl.insertUgroupUser(param);
            SysLogUtil.insertLog("新增用户组用户关联关系",
                    SysLogEnum.SYS_UGROUP_USER.getCode(), SysLogEnum.XZ.getCode(), request);
          /* // if (ret) {
                sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",新增用户组用户成功");
           // }
            //成功记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
        } catch (Exception e) {
            /*sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",新增用户组用户失败");
            //失败记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
            e.printStackTrace();
        }
        int f = ret == true ? 1 : 0;
        ResultObject result = new ResultObject(f, "新增成功", "新增失败");
        return result;
    }

    /**
     * 批量删除用户组用户
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteUgroupUser", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject deleteUgroupUser(String[] ids, HttpServletRequest request) throws TransactionalException {
        Map<String, Object> param = RequestUtils.paramToMap(request);
        List<String> idsList = Arrays.asList(ids);
        param.put("idsList", idsList);
        boolean flag = false;
       /* //获取session中用户
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        SysLog sysLog = new SysLog();
        Date date = new Date();
        sysLog.setUserName(user.getUserName());
        sysLog.setBrowserType(BrowserUtils.getBrowserName(request));
        sysLog.setOperationDate(date);*/
        try {
            //删除用户
            flag = sysUgroupServiceImpl.deleteUgroupUserBatch(param);
            SysLogUtil.insertLog("删除用户组",
                    SysLogEnum.SYS_UGROUP.getCode(), SysLogEnum.SC.getCode(), request);
            //if (count > 0) {
                /*sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",删除用户组用户成功");
           // }
            //成功记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
        } catch (Exception e) {
            /*sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",删除用户组用户失败");
            //失败记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
            e.printStackTrace();
        }
        int f = flag == true ? 1 : 0;
        ResultObject result = new ResultObject(f, "删除用户组用户成功", "删除用户组用户失败");
        return result;
    }

    /**
     * 批量删除用户组
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/batchDeleteUgroup", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject batchDeleteUgroup(String[] ids, HttpServletRequest request) throws TransactionalException {
        Map<String, Object> params = new HashMap<>();
        List<String> idsList = Arrays.asList(ids);
        params.put("idsList", idsList);
        boolean flag = false;
        //获取session中用户
        /*SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        SysLog sysLog = new SysLog();
        Date date = new Date();
        sysLog.setUserName(user.getUserName());
        sysLog.setBrowserType(BrowserUtils.getBrowserName(request));
        sysLog.setOperationDate(date);*/
        try {
            //删除用户
            flag = sysUgroupServiceImpl.batchDeleteUgroup(params);
            SysLogUtil.insertLog("删除用户组",
                    SysLogEnum.SYS_UGROUP.getCode(), SysLogEnum.SC.getCode(), request);

            // if (count > 0) {
            //sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",删除用户组成功");
            // }
            //成功记录操作日志
            //sysLogServiceImpl.insertLog(sysLog);
        } catch (Exception e) {
            //sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",删除用户组失败");
            //失败记录操作日志
            //sysLogServiceImpl.insertLog(sysLog);
            e.printStackTrace();
        }
        int f = flag == true ? 1 : 0;
        ResultObject result = new ResultObject(f, "删除用户组成功", "删除用户组失败");
        return result;
    }

    /**
     * 修改用户组
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/updateUgroup", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject updateUgroup(SysUgroup sysUgroup, HttpServletRequest request) throws TransactionalException {
        boolean flag = false;
        //获取session中用户
        SysUser sysUser = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        /*SysLog sysLog = new SysLog();
        Date date = new Date();
        sysLog.setUserName(sysUser.getUserName());
        sysLog.setBrowserType(BrowserUtils.getBrowserName(request));
        sysLog.setOperationDate(date);*/
        try {
            //更新用户组操作
            sysUgroup.setUpdatorid(String.valueOf(sysUser.getUserId()));
            flag = sysUgroupServiceImpl.updateUgroup(sysUgroup);
            SysLogUtil.insertLog("编辑用户组",
                    SysLogEnum.SYS_UGROUP.getCode(), SysLogEnum.BJ.getCode(), request);
            /*//if (count > 0) {
                sysLog.setOperationContent("用户【"+sysUser.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",修改用户组成功");
           // }
            //成功记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
        } catch (TransactionalException e) {
            /*sysLog.setOperationContent("用户【"+sysUser.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",修改用户组失败");
            //失败记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
            e.printStackTrace();
        }
        int f = flag == true ? 1 : 0;
        ResultObject result = new ResultObject(f, "修改用户组成功", "修改用户组失败");
        return result;
    }

}
